/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufba.lasid.tests;

import br.ufba.lasid.comm.Communicator;
import br.ufba.lasid.comm.api.IReceiver;
import br.ufba.lasid.ds.MembershipView;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author aliriosa
 */
public class TCommunicator {
    
    private static final Log logger = LogFactory.getLog(TCommunicator.class);
    private   static final String at = "@";
    
    public static void main(String[] args) throws Exception{
        
        if(args.length <=0){
            logger.info("Usage: jit-mda-test-communicator <list of ips>");
        }                
        
        Communicator comm = new Communicator();
        
        IReceiver receiver = new IReceiver() {
            @Override
            public void receive(String hostID, String componentID, String message) {
                logger.info("Received [" + message + "] from "  + componentID + at + hostID);
            }
        };        
                
        MembershipView view = new MembershipView();

        for(String argv : args){
            view.add(argv);
        }
//        view.add("192.168.0.81");
//        view.add("192.168.0.80");
        
        String componentID = comm.getName();
        
        for(String hostID : view){
            comm.register(receiver, hostID, componentID);            
            comm.register(receiver, hostID);
        }
        
        for(int i = 0; i < 1000; i++){
            
            for(String hostID : view){
                String message = "[" + i+ "]" +  "Hello from " + hostID;
                comm.send(hostID, message);
                comm.send(hostID, componentID, message);
            }
            
            Thread.sleep(2000);
        }
        
    }
    
    
}
